/***************************************************************************
 *   Copyright (C) 2010 by joker   *
 *   joker@pleiades.pet.cyric.tohoku.ac.jp   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "rdmancls.h"
#include "config.h"

#include <qlineedit.h>
#include <qcheckbox.h>
#include <qpushbutton.h>
#include <kfiledialog.h>
#include <kmessagebox.h>

rdmancls::~rdmancls()
{
delete proc;
}
rdmancls::rdmancls ( QWidget *parent, const char *name )
		:rdManDlg ( parent, name )
{
	proc = new QProcess();
	connect ( proc, SIGNAL ( readyReadStdout() ), this,  SLOT ( slotReadStdout() ) );
	connect ( proc, SIGNAL ( readyReadStderr() ), this,  SLOT ( slotReadStderr() ) );
	connect ( proc, SIGNAL ( processExited() ), this,  SLOT ( connectDone() ) );
	statusLbl->clear();
}

void rdmancls::canConnectSlot()
{
	connectBut->setEnabled ( ( rhEdit->text().length() >0
	                           &&rdEdit->text().length() >0
	                           &&mpEdit->text().length() >0 ) );
}

bool rdmancls::checkDupMount ( QString share )
{
	FILE *f;
	char s[128];
	QString str="";
	f=popen ( "/sbin/mount -t smbfs -p","r" );
	while ( EOF!=fscanf ( f,"%s",&s ) )
	{
		str.append ( s );
		str.append ( " " );
	}
	pclose ( f );
	str.replace ( "//","\n//" );
	int i=str.contains ( share,false );
	if ( i!=0 )
	{
		if ( KMessageBox::No==KMessageBox::warningYesNo ( this,
		        "The share might already be mounted!\n"+str+"\nContinue?",
		        tr ( "Warning" ) ) ) return false;
	}
	return true;
}

void rdmancls:: connectSlot()
{
	connectBut->setEnabled ( false );
	QString share;
	QString node;
	QString passwd;

	errmess="";

	if ( unEdit->text().length() >0 ) share+=unEdit->text() +"@";
	share+=rhEdit->text() +"/";
	share+=rdEdit->text();

	if ( !checkDupMount ( share ) ) return;
	share.prepend ( "//" );

	node=mpEdit->text();

	passwd = pwEdit->text();

	proc->clearArguments();
	proc->addArgument ( "sudo" );
	proc->addArgument ( "mount_smbfs" );
	if ( passwd.length() ==0 )
	{
		proc->addArgument ( "-N" );
	}
	else
	{
		passwd+="\n";
	}
	proc->addArgument ( share );
	proc->addArgument ( node );
	statusLbl->setPaletteForegroundColor("black");
	statusLbl->setText("Connecting...");
	proc->launch ( passwd );
}

void rdmancls::slotReadStdout()
{
}
void rdmancls::slotReadStderr()
{
	while ( proc->canReadLineStderr() )
	{
		errmess.append ( proc->readLineStderr() );
	}
}

void rdmancls:: connectDone()
{
	if ( proc->exitStatus() !=0 )
	{
		KMessageBox::detailedError ( this,"Connection failed",errmess,"ERROR" );
		statusLbl->setPaletteForegroundColor("red");
		statusLbl->setText("Connection failed");
	}
	else
	{
		KMessageBox::information ( this,"connected" );
		statusLbl->setPaletteForegroundColor("blue");
		statusLbl->setText("connected");
	}
}

void rdmancls:: openMountPointSlot()
{
	QString s;
	s=KFileDialog::getExistingDirectory ( "",this,tr ( "Choose a mount point" ) );
	if ( s.length() !=0 )
	{
		mpEdit->setText ( s );
		if ( setDefaultDataPathBox->isChecked() ) datapath->setText ( s );
	}
}

void rdmancls:: setDefaultDataPathSlot()
{
	if ( setDefaultDataPathBox->isChecked() &&mpEdit->text().length() >0 ) datapath->setText ( mpEdit->text() );
}